/**
 * https://leetcode.cn/problems/can-place-flowers/
 */
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
  for (let i = 0; i < flowerbed.length; i++) {
    const current = flowerbed[i];
    const prev = flowerbed[i - 1] ?? -1;
    const next = flowerbed[i + 1] ?? -1;

    if (n === 0) return true;

    if (current === 0 && prev === -1 && next === -1) {
      n--;
      continue;
    }

    if (current === 0 && prev === 0 && next === 0) {
      flowerbed[i] = 1;
      n--;
      continue;
    }

    if (i === 0 && next === 0 && current === 0) {
      n--;
      i++;
      continue;
    }

    if (i === flowerbed.length - 1 && current === 0 && prev === 0) {
      n--;
      continue;
    }
  }

  return n === 0 ? true : false;
}
